﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Assertions;
using UnityEngine.UI;

namespace Nirvana
{
    [RequireComponent(typeof(Text))]
    [AddComponentMenu("Nirvana/UI/Bind/Variable Bind Text")]
    public sealed class UIVariableBindText : UIVariableBind
    {
        private static Logger logger = LogSystem.GetLogger("UIVariableBindText");
        [SerializeField]
        [Delayed]
        [TextArea(2, 10)]
        private string format;

        [VariableName(UIVariableType.Boolean, UIVariableType.Integer, UIVariableType.Float, UIVariableType.String)]
        [SerializeField]
        private string[] paramBinds;
        private Text _text;
        private UIVariable[] _uivariables;

        public string Format
        {
            get
            {
                return this.format;
            }
            set
            {
                if (this.format != value)
                {
                    this.format = value;
                    this.InitText();
                }
            }
        }

        private void Awake()
        {
            this.Binds();
            this._text = base.GetComponent<Text>();
            if (this._text == null)
            {
                return;
            }
        }

        protected override void BindVariables()
        {
            Assert.IsNull<UIVariable[]>(this._uivariables);
            if (this.paramBinds != null && this.paramBinds.Length > 0)
            {
                this._uivariables = new UIVariable[this.paramBinds.Length];
                for (int i = 0; i < this.paramBinds.Length; i++)
                {
                    string text = this.paramBinds[i];
                    if (!string.IsNullOrEmpty(text))
                    {
                        UIVariable uivariable = base.FindVariable(text);
                        if (uivariable == null)
                        {
                            UIVariableBindText.logger.LogWarning("{0} can not find a variable {1}", new object[]
                            {
                                base.name,
                                text
                            });
                        }
                        else
                        {
                            uivariable.OnValueInitialized += this.InitText;
                            uivariable.OnValueChanged += this.InitText;
                            uivariable.Add(this);
                            this._uivariables[i] = uivariable;
                        }
                    }
                }
                this.InitText();
            }
        }

        protected override void UnbindVariables()
        {
            if (this._uivariables != null)
            {
                foreach (UIVariable uivariable in this._uivariables)
                {
                    if (uivariable != null)
                    {
                        uivariable.OnValueInitialized -= this.InitText;
                        uivariable.OnValueChanged -= this.InitText;
                        uivariable.Remove(this);
                    }
                }
                this._uivariables = null;
            }
        }

        private void InitText()
        {
            if (this._text == null)
            {
                this._text = base.GetComponent<Text>();
            }
            if (this._text == null || this.paramBinds == null || this._uivariables == null)
            {
                return;
            }
            if (string.IsNullOrEmpty(this.format))
            {
                if (this.paramBinds.Length > 0)
                {
                    UIVariable uivariable = this._uivariables[0];
                    if (uivariable != null)
                    {
                        object valueObject = uivariable.ValueObject;
                        if (valueObject != null)
                        {
                            this._text.text = valueObject.ToString();
                        }
                    }
                }
            }
            else
            {
                object[] array = new object[this.paramBinds.Length];
                for (int i = 0; i < this.paramBinds.Length; i++)
                {
                    UIVariable uivariable2 = this._uivariables[i];
                    if (uivariable2 != null)
                    {
                        array[i] = uivariable2.ValueObject;
                    }
                }
                try
                {
                    this._text.text = string.Format(this.format, array);
                }
                catch (FormatException ex)
                {
                    if (Application.isPlaying)
                    {
                        Debug.LogError(ex.Message, this);
                    }
                }
            }
        }

    }
}


